package com.yuerenjf.yuerenjinfu.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.qiniu.android.utils.StringUtils;
import com.special.ResideMenu.ResideMenu;
import com.umeng.analytics.MobclickAgent;
import com.yuerenjf.yuerenjinfu.R;
import com.yuerenjf.yuerenjinfu.bean.UpdateInfo;
import com.yuerenjf.yuerenjinfu.dialog.DialogUpdata;
import com.yuerenjf.yuerenjinfu.fragment.MainTabFragment;
import com.yuerenjf.yuerenjinfu.http.Constants;
import com.yuerenjf.yuerenjinfu.http.Xutils;
import com.yuerenjf.yuerenjinfu.utils.CacheUtils;
import com.yuerenjf.yuerenjinfu.utils.CommonUtils;
import com.yuerenjf.yuerenjinfu.utils.ConstantUtils;
import com.yuerenjf.yuerenjinfu.utils.MyDisplayImageOptions;
import com.yuerenjf.yuerenjinfu.utils.PhoneUtils;

import org.json.JSONObject;
import org.xutils.common.util.LogUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends FragmentActivity{

    private ResideMenu resideMenu;
    private MainActivity mContext;
    private com.yuerenjf.yuerenjinfu.widget.CircleImageView mRight_menu_imageView;
    private TextView mRight_menu_login_text;
    public LinearLayout mMenu_right_main_layout;
    public TextView mMenu_right_main_text;
    public LinearLayout mMenu_right_list_layout;
    public TextView mMenu_right_list_text;
    private LinearLayout mMenu_right_lend_layout;
    private LinearLayout mMenu_right_user_center_layout;
    private LinearLayout mMenu_right_share_layout;
    private LinearLayout mMenu_right_charge_layout;
    private LinearLayout mMenu_right_tixian_layout;
    private LinearLayout mMenu_right_huankaun_layout;
    private LinearLayout mMenu_right_help_layout;
    private LinearLayout mMenu_right_about_us_layout;
    private TextView mMenu_right_register_text;
    private TextView mMenu_right_phone_text;
    private LinearLayout mMenuRightCallLayout;
    private RelativeLayout menu_right_width_layout;

    private String[] fragmentTag = {"mainTab", "login", "register", "retrice"};

    public FragmentManager getFm() {
        return fm;
    }

    private FragmentManager fm;

    public MainTabFragment getContentFragment() {
        return contentFragment;
    }

    public void setContentFragment(MainTabFragment contentFragment) {
        this.contentFragment = contentFragment;
    }

    private MainTabFragment contentFragment;

    public Fragment getmCurrentFragment() {
        return mCurrentFragment;
    }

    public Fragment mCurrentFragment;


    /**
     * 检查升级成功
     */
    protected static final int WHAT_REQUEST_UPDATE_SUCCESS = 3;

    /**
     * 下载apk成功
     */
    private static final int WHAT_DOWNLOAD_SUCCESS = 5;

    /**
     * 下载apk失败
     */
    private static final int WHAT_DOWNLOAD_ERROR = 6;
    /**
     * 版本升级信息
     */
    private UpdateInfo updateInfo;

    private File apkFile;

    /**
     * 升级提示Dialog
     */
    private ProgressDialog pd;
    DialogUpdata updateDialog;
    public static final String TAG = GuideActivity.class.getSimpleName();


    private TextView mTitleTextView;
    private ImageView mLogoImage;

    public void setmTitleTextView(String title) {
        mTitleTextView.setText(title);
    }

    public void setLogoImageShow(boolean show) {
        mLogoImage.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTitleTextView = (TextView) findViewById(R.id.activity_main_title_text);
        mLogoImage = (ImageView) findViewById(R.id.title_bar_left_menu);
        fm = getSupportFragmentManager();
        setUpMenu();
        mContext = this;

        if(getIntent().hasExtra("type")){
            contentFragment = MainTabFragment.getInstance(1);
        }else{
            contentFragment = MainTabFragment.getInstance(0);
        }


        if (savedInstanceState == null) {

            mCurrentFragment = contentFragment;
            changeFragment(contentFragment);
        }


        if (ConstantUtils.HEIGHT_SCREEN <= 0) {
            ConstantUtils.initiScreenParam(this);
        }

//        PushAgent mPushAgent = PushAgent.getInstance(this);
//        mPushAgent.enable();
//        mPushAgent.onAppStart();
//        String device_token = UmengRegistrar.getRegistrationId(this)
// ;
//        Log.e("device_token",device_token+"-------------");

        //初始化sharesdk


        checkUpdate();


    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        LogUtil.i("onNewIntent");
        setIntent(intent);
        if (intent.hasExtra("current")) {
            if (intent.getStringExtra("current").equals("0")) {//退出登录
                switchContent(mCurrentFragment, contentFragment, fragmentTag[0]);
                contentFragment.setTabSelected(0);
                resideMenu.closeMenu();
            } else if (intent.getStringExtra("current").equals("3")) {//
                switchContent(mCurrentFragment, contentFragment, fragmentTag[3]);
                contentFragment.setTabSelected(3);
                resideMenu.closeMenu();
            }else if (intent.getStringExtra("current").equals("1")) {//
                switchContent(mCurrentFragment, contentFragment, fragmentTag[1]);
                contentFragment.setTabSelected(1);
                resideMenu.closeMenu();
            }else if (intent.getStringExtra("current").equals("2")) {//
                switchContent(mCurrentFragment, contentFragment, fragmentTag[2]);
                contentFragment.setTabSelected(2);
                resideMenu.closeMenu();
            }else if(intent.getStringExtra("current").equals("5")){
                switchContent(mCurrentFragment, contentFragment, fragmentTag[3]);
                contentFragment.setTabSelected(3);
                resideMenu.closeMenu();
                Intent intent2 =new Intent(MainActivity.this,SettingActivity.class);
                startActivity(intent2);
            }
        }


    }

    /**
     * 检查升级
     */
    private void checkUpdate() {

        Map<String, String> map = new HashMap<String, String>();
        map.put("version", PhoneUtils.getAppVersionName(MainActivity.this));
        LogUtil.i(PhoneUtils.getAppVersionName(MainActivity.this));
        Xutils.getInstance().post(MainActivity.this, Constants.URL_GET_UPDATA, map, new Xutils.XCallBack() {
            @Override
            public void onResponse(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONObject object = jsonObject.getJSONObject("data");
                    if (object != null) {
                        String url = object.optString("url");
//                        url="http://oqjq4768c.bkt.clouddn.com/zpjf_1.0.1.apk";
                        String info = object.optString("info");
                        String version = object.optString("version");
                        boolean abort = object.optBoolean("abort");

                        showHintDownloadDialog(url,info,version,false);

                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailer(int status, String result) {

            }

            @Override
            public void onFinish() {

            }
        });
    }

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {// 定义一个Handler，用于处理下载线程与UI间通讯
            super.handleMessage(msg);
            switch (msg.what) {
                case WHAT_REQUEST_UPDATE_SUCCESS:
                    if (!CommonUtils.getVersion(MainActivity.this).equals(updateInfo.getVersion())
                            && System.currentTimeMillis() - CacheUtils.getLong(MainActivity.this, ConstantUtils.SP_SYS_TIME) > 86400000) {
//                        showHintDownloadDialog();
                    }
                    break;
                case WHAT_DOWNLOAD_SUCCESS:
                    pd.dismiss();
                    installApk();
                    break;
                case WHAT_DOWNLOAD_ERROR:
                    pd.dismiss();
                    CommonUtils.showMessage(getApplicationContext(), "下载apk失败");
                    break;
                default:
                    break;
            }
        }

    };

    /**
     * 安装apk文件
     */
    private void installApk() {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        startActivity(intent);
    }

    /**
     * 更新Dialog
     */
    private void showHintDownloadDialog(final String url,String content,String version,boolean isAbort) {


        updateDialog=new DialogUpdata(MainActivity.this,isAbort);
        updateDialog.showCustomDialog();
        updateDialog.setCotent(version,content);
        updateDialog.setCallBack(new DialogUpdata.TypeCallBack() {
            @Override
            public void setCancel() {
                if(updateDialog!=null&&updateDialog.isShowing()){
                    updateDialog.dismiss();
                }
            }

            @Override
            public void setUpdata() {
                if(updateDialog!=null&&updateDialog.isShowing()){
                    updateDialog.dismiss();
                }
                startDownloadApk(url);

            }
        });
        updateDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if(updateDialog.isAbort()){
                    if(updateDialog!=null&&updateDialog.isShowing()){
                        updateDialog.dismiss();
                    }
                    finish();
                }
            }
        });

    }

    /**
     * 下载apk
     */
    private void startDownloadApk(final String url) {
        apkFile = new File(getExternalFilesDir(null), getFileName(url));
        pd = new ProgressDialog(this);
        pd.setCancelable(false);
        pd.setCanceledOnTouchOutside(false);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setProgressNumberFormat("%1d kb/%2d kb");
        pd.show();

        new Thread() {
            public void run() {
                try {
                    CommonUtils.downloadApk(url, apkFile, pd);
                    handler.sendEmptyMessage(WHAT_DOWNLOAD_SUCCESS);
                } catch (Exception e) {
                    handler.sendEmptyMessage(WHAT_DOWNLOAD_ERROR);
                }
            }
        }.start();


    }

//    @Override
//    public void onBackPressed() {
//        super.onBackPressed();
//        Intent intent = new Intent(Intent.ACTION_MAIN);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        intent.addCategory(Intent.CATEGORY_HOME);
//        startActivity(intent);
//    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        if (CacheUtils.isNeedLogin(MainActivity.this)) {
            mRight_menu_imageView.setImageResource(R.drawable.icon_right_menu_touxiang);
            mMenu_right_register_text.setVisibility(View.VISIBLE);
            mMenu_right_phone_text.setVisibility(View.GONE);
            mRight_menu_login_text.setVisibility(View.VISIBLE);
        } else {
            ImageLoader.getInstance().displayImage(CacheUtils.getString(MainActivity.this,ConstantUtils.SP_AVATAR), mRight_menu_imageView, MyDisplayImageOptions.getInstance(R.drawable.icon_header_default));
            String userPhone = CacheUtils.getString(MainActivity.this, ConstantUtils.SP_USER_PHONE);
            userPhone = userPhone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
            mMenu_right_phone_text.setText(userPhone);
            mMenu_right_register_text.setVisibility(View.GONE);
            mMenu_right_phone_text.setVisibility(View.VISIBLE);
            mRight_menu_login_text.setVisibility(View.GONE);
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        /** attention to this below ,must add this**/
//        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
//    }

    private void setUpMenu() {

        // attach to current activity;
        resideMenu = new ResideMenu(this, -1, R.layout.menu_right_view);
        resideMenu.setBackground(R.color.colorPrimaryDanmu);
        resideMenu.attachToActivity(this);
        resideMenu.setMenuListener(menuListener);
        resideMenu.setShadowVisible(false);
        //valid scale factor is between 0.0f and 1.0f. leftmenu'width is 150dip.
        resideMenu.setScaleValue(0.7f);
//        resideMenu.setScaleValueY(0.8f);
        resideMenu.setSwipeDirectionDisable(resideMenu.DIRECTION_LEFT);
        setRightMenu();
        final View rightMenu = resideMenu.getRightMenuView();
        ViewGroup.LayoutParams layout = rightMenu.getLayoutParams();
        DisplayMetrics dm = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int widthPixels=dm.widthPixels;
        layout.width =ViewGroup.LayoutParams.MATCH_PARENT;

        layout.height = ViewGroup.LayoutParams.MATCH_PARENT;

        rightMenu.setLayoutParams(layout);
        LinearLayout.LayoutParams width_layout = (LinearLayout.LayoutParams) menu_right_width_layout.getLayoutParams();
        width_layout.width= (int) (widthPixels*0.45);
        width_layout.height = ViewGroup.LayoutParams.MATCH_PARENT;
        menu_right_width_layout.setLayoutParams(width_layout);


        findViewById(R.id.title_bar_right_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resideMenu.openMenu(ResideMenu.DIRECTION_RIGHT);
            }
        });
    }


    private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {
        @Override
        public void openMenu() {
            new Handler().postDelayed(new Runnable(){
                public void run() {
                    menu_right_width_layout.setVisibility(View.VISIBLE);
                }
            }, 250);

        }

        @Override
        public void closeMenu() {
            menu_right_width_layout.setVisibility(View.GONE);
        }
    };

    private void changeFragment(Fragment targetFragment) {
        resideMenu.clearIgnoredViewList();
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.main_fragment, targetFragment, "mainTab")
                .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                .commitAllowingStateLoss();
    }

    // What good method is to access resideMenu？
    public ResideMenu getResideMenu() {
        return resideMenu;
    }


    private void setRightMenu() {
        View view = resideMenu.getRightMenuView();


        bindViews(view);


    }


    private void bindViews(View view) {
        menu_right_width_layout= (RelativeLayout) view.findViewById(R.id.menu_right_width_layout);

        mRight_menu_imageView = (com.yuerenjf.yuerenjinfu.widget.CircleImageView) view.findViewById(R.id.right_menu_imageView);
        mRight_menu_login_text = (TextView) view.findViewById(R.id.right_menu_login_text);
        mMenu_right_main_layout = (LinearLayout) view.findViewById(R.id.menu_right_main_layout);
        mMenu_right_list_layout = (LinearLayout) view.findViewById(R.id.menu_right_list_layout);
        mMenu_right_lend_layout = (LinearLayout) view.findViewById(R.id.menu_right_lend_layout);
        mMenu_right_user_center_layout = (LinearLayout) view.findViewById(R.id.menu_right_user_center_layout);
        mMenu_right_share_layout = (LinearLayout) view.findViewById(R.id.menu_right_share_layout);
        mMenu_right_charge_layout = (LinearLayout) view.findViewById(R.id.menu_right_charge_layout);
        mMenu_right_tixian_layout = (LinearLayout) view.findViewById(R.id.menu_right_tixian_layout);
        mMenu_right_huankaun_layout = (LinearLayout) view.findViewById(R.id.menu_right_huankaun_layout);
        mMenu_right_help_layout = (LinearLayout) view.findViewById(R.id.menu_right_help_layout);
        mMenu_right_about_us_layout = (LinearLayout) view.findViewById(R.id.menu_right_about_us_layout);
        mMenu_right_register_text = (TextView) view.findViewById(R.id.menu_right_register_text);
        mMenu_right_phone_text = (TextView) view.findViewById(R.id.right_menu_phone_text);
        mMenu_right_main_text= (TextView) view.findViewById(R.id.menu_right_main_text);
        mMenu_right_list_text= (TextView) view.findViewById(R.id.menu_right_list_text);

        mMenuRightCallLayout= (LinearLayout) view.findViewById(R.id.bottom_right_call_layout);
        mRight_menu_login_text.setOnClickListener(onClickListener);
        mMenu_right_main_layout.setOnClickListener(onClickListener);
        mMenu_right_list_layout.setOnClickListener(onClickListener);
        mMenu_right_lend_layout.setOnClickListener(onClickListener);
        mMenu_right_user_center_layout.setOnClickListener(onClickListener);
        mMenu_right_share_layout.setOnClickListener(onClickListener);
        mMenu_right_charge_layout.setOnClickListener(onClickListener);
        mMenu_right_tixian_layout.setOnClickListener(onClickListener);
        mMenu_right_huankaun_layout.setOnClickListener(onClickListener);
        mMenu_right_help_layout.setOnClickListener(onClickListener);
        mMenu_right_about_us_layout.setOnClickListener(onClickListener);
        mMenuRightCallLayout.setOnClickListener(onClickListener);
        mMenu_right_register_text.setOnClickListener(onClickListener);
    }


    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.right_menu_imageView:
                    break;
                case R.id.menu_right_register_text:
                    Intent intent2=new Intent(MainActivity.this,RegisterActivity.class);
                    startActivity(intent2);

                    break;

                case R.id.menu_right_user_center_layout://个人中心
                    if (CacheUtils.isNeedLogin(MainActivity.this)) {
                        LoginActivity.launchLoginActivity(MainActivity.this, LoginActivity.FROM_TYPE_MAIN_TAB);
                    } else {
                        switchContent(mCurrentFragment, contentFragment, "retrice");
                        contentFragment.setTabSelected(3);
                        resideMenu.closeMenu();
                    }

                case R.id.right_menu_login_text:
//                    switchContent(mCurrentFragment,new LoginFragment(),"login");
//                    resideMenu.closeMenu();
//                    changeFragment(new LoginFragment());
                    LoginActivity.launchLoginActivity(MainActivity.this, LoginActivity.FROM_TYPE_MAIN_TAB);
                    resideMenu.closeMenu();
                    break;

                case R.id.menu_right_main_layout:
                    switchContent(mCurrentFragment, contentFragment, "mainTab");
                    contentFragment.setTabSelected(0);
                    resideMenu.closeMenu();

                    mMenu_right_main_layout.setBackgroundColor(getResources().getColor(R.color.app_theme_color));
                    mMenu_right_list_layout.setBackground(null);
                    break;

                case R.id.menu_right_list_layout:
                    switchContent(mCurrentFragment, contentFragment, "mainTab");
                    contentFragment.setTabSelected(1);

                    resideMenu.closeMenu();

                    break;

                case R.id.menu_right_lend_layout://我要借款

                    LogUtil.i(CacheUtils.getString(MainActivity.this, ConstantUtils.SP_UID));
                    if (StringUtils.isNullOrEmpty(CacheUtils.getString(MainActivity.this, ConstantUtils.SP_UID))) {
                        LoginActivity.launchLoginActivity(MainActivity.this, Constants.URL_JIEKUAN);
                    } else {
                        WebActivity.launchActivity(MainActivity.this, Constants.URL_JIEKUAN);
                    }

                    resideMenu.closeMenu();
                    mMenu_right_list_layout.setBackground(null);
                    mMenu_right_main_layout.setBackground(null);
                    break;

                case R.id.menu_right_charge_layout://充值
                    if (CacheUtils.isNeedLogin(MainActivity.this)) {
                        LoginActivity.launchLoginActivity(MainActivity.this, Constants.URL_RECHARGE);
                    } else {
                        WebActivity.launchActivity(MainActivity.this, Constants.URL_RECHARGE);
                    }
                    resideMenu.closeMenu();
                    break;

                case R.id.menu_right_share_layout:
                    WebActivity.launchActivity(MainActivity.this, Constants.URL_YAOQINGHAOYOU);
                    resideMenu.closeMenu();
                    break;

                case R.id.menu_right_tixian_layout:
                    if (CacheUtils.isNeedLogin(MainActivity.this)) {
                        LoginActivity.launchLoginActivity(MainActivity.this, Constants.URL_TIXIAN);
                    } else {
                        WebActivity.launchActivity(MainActivity.this, Constants.URL_TIXIAN);
                    }
                    resideMenu.closeMenu();
                    break;

                case R.id.menu_right_huankaun_layout:
                    if (CacheUtils.isNeedLogin(MainActivity.this)) {
                        LoginActivity.launchLoginActivity(MainActivity.this,Constants.URL_HUANKUAN);
                    } else {
                        WebActivity.launchActivity(MainActivity.this, Constants.URL_HUANKUAN);
                    }
                    resideMenu.closeMenu();
                    break;

                case R.id.menu_right_help_layout:
                    WebActivity.launchActivity(MainActivity.this, Constants.URL_HELP_CENTER);
                    resideMenu.closeMenu();
                    break;

                case R.id.menu_right_about_us_layout:
                    WebActivity.launchActivity(MainActivity.this, Constants.URL_ABOUT_US);
                    resideMenu.closeMenu();
                    break;

                case R.id.bottom_right_call_layout:

                    Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:4008024999"));
                    startActivity(intent);
                    break;

            }

            int selectedPosition=contentFragment.getTabSelected();

            if(selectedPosition==0){
                mMenu_right_main_layout.setBackgroundColor(getResources().getColor(R.color.app_theme_color));
                mMenu_right_list_layout.setBackground(null);
                mMenu_right_main_text.setTextColor(getResources().getColor(R.color.white));
                mMenu_right_list_text.setTextColor(Color.parseColor("#adabac"));
            }else if(selectedPosition==1){
                mMenu_right_list_layout.setBackgroundColor(getResources().getColor(R.color.app_theme_color));
                mMenu_right_main_layout.setBackground(null);
                mMenu_right_list_text.setTextColor(getResources().getColor(R.color.white));
                mMenu_right_main_text.setTextColor(Color.parseColor("#adabac"));
            }else{
                mMenu_right_list_layout.setBackground(null);
                mMenu_right_main_layout.setBackground(null);
                mMenu_right_list_text.setTextColor(Color.parseColor("#adabac"));
                mMenu_right_main_text.setTextColor(Color.parseColor("#adabac"));
            }
        }
    };


    /**
     * fragment 切换
     *
     * @param from
     * @param to
     */
    public void switchContent(Fragment from, Fragment to, String tag) {
        if (mCurrentFragment != to) {
            mCurrentFragment = to;
            FragmentTransaction transaction = fm.beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                transaction.hide(from)
                        .add(R.id.main_fragment, to, tag).addToBackStack(null).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(from).show(to).addToBackStack(null).commit(); // 隐藏当前的fragment，显示下一个
            }
        }
    }
    public String getFileName(String pathandname){
        int start=pathandname.lastIndexOf("/");
        int end=pathandname.lastIndexOf(".");
        if (start!=-1 && end!=-1) {
            return pathandname.substring(start+1, end);
        }
        else {
            return null;
        }
    }

//
//    @Override
//    public void onBackPressed() {
//        super.onBackPressed();
//        LogUtil.i("onBackPressed");
//
//    }


//    public boolean dispatchKeyEvent(android.view.KeyEvent event) {
//        switch(event.getKeyCode())
//        {
//            case KeyEvent.KEYCODE_BACK:
//            {
//                if(updateDialog!=null&&updateDialog.isShowing()&&updateDialog.isAbort()){
//                    MainActivity.this.finish();
//                }
//                return false;
//            }
//        }
//        return true;
//    };
}
